/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package avl.sv.shared.Annotations;

import java.awt.Point;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;

/**
 *
 * @author benbryan
 */
public class ROIRectangle extends ROI {

    private Polygon poly;
    private boolean creating, fixedSize;
    public static final int TYPE = 1;
    
    public ROIRectangle() {
    }
    
    public static ROIRectangle getDefault() {
        ROIRectangle r = new ROIRectangle();
        r.attributes = new ArrayList<>();
        r.text = "default name";
        r.selectedPoints = new ArrayList<>();
        r.fixedSize = false;
        return r;
    }
    
    public ROIRectangle(int lx, int ly, int width, int height) {
        attributes = new ArrayList<>();
        text = "default name";
        selectedPoints = new ArrayList<>();
        
        int ux = lx + width;
        int uy = ly + height;
        poly = new Polygon();
        poly = new Polygon();
        poly.addPoint(lx, ly);
        poly.addPoint(lx, uy);
        poly.addPoint(ux, uy);
        poly.addPoint(ux, ly);
        creating = false;
        fixedSize = true;
    }

    public ROIRectangle(int ix, int iy) {
        attributes = new ArrayList<>();
        text = "default name";
        selectedPoints = new ArrayList<>();
        
        poly = new Polygon();
        poly.addPoint(ix, iy);
        creating = true;
        fixedSize = false;
    }

    public Polygon getPoly() {
        return poly;
    }

    public boolean isCreating() {
        return creating;
    }

    public boolean isFixedSize() {
        return fixedSize;
    }

    public static int getTYPE() {
        return TYPE;
    }

    public void setPoly(Polygon poly) {
        this.poly = poly;
    }

    public void setCreating(boolean creating) {
        this.creating = creating;
    }

    public void setFixedSize(boolean fixedSize) {
        this.fixedSize = fixedSize;
    }

    @Override
    public void addPoint(int x, int y) {
        if (creating == false) {
            return;
        }
        poly.addPoint(x, y);
        if (poly.npoints == 4) {
            creating = false;
        }
    }
    @Override
    public void addPoint(double x, double y) {
        addPoint((int)x, (int)y);
    }
    
    public boolean addSecondPoint(int nx, int ny){
        if (creating == false){
            return false;
        } else {
            int ix = poly.xpoints[0];
            int iy = poly.ypoints[0];
            int[] x = new int[]{ix,nx,nx,ix};
            int[] y = new int[]{iy,iy,ny,ny};
            poly.xpoints = x;
            poly.ypoints = y;
            poly.npoints = 4;
            return true;
        }
    }
    
    public void closeRectangle(){
        creating = false;
    }
    
    @Override
    public Polygon getPolygon(AffineTransform at){     
        return poly;
    }
    
    @Override
    public void changePoint(int index, double nx, double ny){
        changePoint(index, (int) nx, (int) ny);
    }

    @Override
    public void changePoint(int index, int nx, int ny) {
        int ox = poly.xpoints[index];
        int oy = poly.ypoints[index];
        if (fixedSize) {
            poly.translate(nx-ox, ny-oy);
        } else {
            for (int i = 0; i < poly.npoints; i++) {
                if (poly.xpoints[i] == ox) {
                    poly.xpoints[i] = nx;
                }
                if (poly.ypoints[i] == oy) {
                    poly.ypoints[i] = ny;
                }
            }
            poly.xpoints[index] = nx;
            poly.ypoints[index] = ny;
        }

    }

    @Override
    public boolean containsPoint(int x, int y) {
        return poly.contains(x, y);
    }
    
    @Override
    public boolean containsPoint(Point p) {
        return poly.contains(p);
    }

    @Override
    public Shape getShape() {
        return poly;
    }
    
    @Override
    public int getType() {
        return TYPE;
    }
    
    @Override
    public Object clone() {
        ROIRectangle temp = (ROIRectangle) super.clone();
        temp.poly = new Polygon();
        temp.poly.npoints = poly.npoints;
        temp.poly.xpoints = new int[temp.poly.npoints];
        temp.poly.ypoints = new int[temp.poly.npoints];
        for (int i = 0; i < poly.npoints; i++){
            temp.poly.xpoints[i] = poly.xpoints[i];
            temp.poly.ypoints[i] = poly.ypoints[i];
        }
        return temp;
    }
    
}
